from sys import unraisablehook
from API.get import *
import urllib.request,json,os,re
from urllib.request import urlretrieve
#镜像源
url = {
    "Mojang":"http://launchermeta.mojang.com/mc/game/version_manifest.json",
    "bmclapi":"https://bmclapi2.bangbang93.com/mc/game/version_manifest.json",
    "mcbbs":"https://download.mcbbs.net/",
    "mcinal":"https://mcinalgitpor.cn"
}
#mcinal 云计算
version = json.load(headers_save(url['Mojang']))
# 遍历JSon对象     #用户选择的版本
select = '1.17.1-rc1'

def Dowdload_JAR(url_json):
    #Path 获取当前工作路径
    path = os.getcwd()
    Ispath = ['.minecraft','.minecraft\\versions','.minecraft\\versions\\'+select]
    #首先判断文件夹
    for ispath in Ispath:
        if os.path.exists(path+ "\\" + ispath) == False:
            os.mkdir(path + "\\" +ispath)

    #爬取Url_Json文件
    VersionsJson = headers_save(url_json).read().decode('UTF-8')
    # print(Versions_Json.read().decode('UTF-8'))
    with open(os.getcwd() + '\\.minecraft\\versions\\' + select + '\\' + select + ".json",'w+') as fs:
        fs.write(VersionsJson)
    print('*********-版本JSON-********')
    
    #以下是客户端jar下载
    clinetversion = json.loads(str(VersionsJson))
    Clinet = clinetversion['downloads']['client']['url']
    MinecraftVersionFile = headers_save(Clinet).read()
    with open(os.getcwd() + '\\.minecraft\\versions\\' + select + '\\' + select + ".jar",'wb') as fs:
        fs.write(MinecraftVersionFile)
    print('*********-客户端JAR-********')

    #assets/indexes资源下载
    assets_json = json.loads(str(VersionsJson))
    assets = assets_json['assetIndex']['url']
    assets_cwd = os.getcwd()
    assetspath = ['.minecraft','.minecraft\\assets','.minecraft\\assets\\indexes','.minecraft\\assets\\objects']
    for path in assetspath:
        if os.path.exists(assets_cwd +"\\"+ path) == False:
            os.mkdir(assets_cwd +"\\"+ path)
    assets_file_Json = headers_save(assets).read().decode('UTF-8')        
    with open(os.getcwd()+'\\'+'.minecraft\\assets\\indexes\\'+select+'.json','w+') as wites:
        wites.write(assets_file_Json)  
    print('*********-Assets Json文件-********')

    #assets/objects资源下载
    ob = json.loads(str(assets_file_Json))
    for obj in ob['objects']:
        hashs = ob['objects'][obj]['hash']
        hash_before_two = hashs[0] + hashs[1]
        objects_download_path = 'http://resources.download.minecraft.net/' + hash_before_two + '/' + hashs
        if os.path.exists(objects_download_path) == False:
            os.mkdir(objects_download_path)
        objects_download = headers_save(objects_download_path).read()
        with open(objects_download_path+'\\'+ hashs,'wb') as ao:
            ao.write(objects_download)
        print('*********-Assets文件-********')


    #libraries资源下载
    for libraries in range(len(clinetversion['libraries'])):
        for librariesr in clinetversion['libraries'][libraries]['downloads']:
            # print(clinetversion['libraries'][libraries]['downloads'][librariesr])
            rearp = re.compile(r"url': '([^\']+)")
            Aboutjson = rearp.findall(str(clinetversion['libraries'][libraries]['downloads'][librariesr]),7)
            for num in Aboutjson:
                filter_path = re.findall(r'(?<=net/).*$',num)
                # os.getcwd() + path New Files
                http_data = headers_save(num).read()
            #with open(os.getcwd()+'\\.minecraft\\libraries\\'+filter_path[0],'w+') as ee:
                #ee.write(http_data)

           

    
# 遍历对象内的 obj(对象) => latest,versions
for cout in version['versions']:
    # versions > id,type,url,time,releaseTime
    if cout['id'] == select:
        Dowdload_JAR(cout['url'])
        #找到了就直接出去，遍历完成费力，不如直接出去
        break